/**
 * CircleDao.java
 * com.jamin.aabill.dao
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-27 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jamin.aabill.data.Bill;
import com.jamin.aabill.data.Circle;
import com.jamin.aabill.data.CircleList;
import com.jamin.aabill.db.BillDataBaseHelper;
import com.jamin.aabill.db.BillDetailTableMetaData;
import com.jamin.aabill.db.BillTableMetaData;
import com.jamin.aabill.db.CircleMemberTableMetaData;
import com.jamin.aabill.db.CircleTableMetaData;
import com.jamin.aabill.ui.AABillApplication;

/**
 * ClassName:CircleDao
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-27 下午2:56:17 
 */
public class CircleDao {


	private static final String TAG = "CircleDao";
	BillDataBaseHelper mDbHelper = null;


	public CircleDao(){
		mDbHelper = AABillApplication.mDB;
	}


	/**
	 * 插入用户数据<br>
	 * @param userInfo
	 */
	public long replease(Circle circle){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		long id = db.replace(CircleTableMetaData.TABLE_NAME, null, getContentValues(circle));
		db.close();
		return id;
	}

	

	/**
	 * 查询单个圈子信息<br>
	 * @return
	 */
	public Circle getCircle(long id){
		Circle mCircle = null;
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor mCur = db.query(CircleTableMetaData.TABLE_NAME,
				null,CircleTableMetaData._ID + "=" + id, null, null, null, null);
		if(mCur != null ){
			if(mCur.getCount() != 0){
				mCur.moveToFirst();
				return new Circle(mCur);
			}
			mCur.close();
		}
		return mCircle;
	}


	/**
	 * 得到圈子列表<br>
	 * @method getCircleList
	 * @param id
	 * @return
	 * @throws 
	 * @since v1.0
	 */
	public List<Circle> getCircleList(){
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor mCur = db.query(CircleTableMetaData.TABLE_NAME, 
				null,null, null, null, null, CircleTableMetaData.CREATE_AT + " desc");
		if(mCur != null ){
			if(mCur.getCount() != 0){
				return new CircleList(mCur).getList();
			}
			mCur.close();
		}
		return new ArrayList<Circle>();
	}

	/**
	 * 删除数据库<br>
	 */
	public void deleteCircle(long circleId , List<Bill> list){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		db.beginTransaction();
		try{
			//删除圈子中的所有BILL和BILLDETAIL
			int size = list.size();
			for(int i = 0 ; i < size; i ++){
				long billId = list.get(i).getBILL_ID();
				db.delete(BillDetailTableMetaData.TABLE_NAME ,BillDetailTableMetaData.BILL_ID + "=" + billId, null);
				db.delete(BillTableMetaData.TABLE_NAME ,BillTableMetaData._ID + "=" + billId, null);
			}
			//删除CIRCLE
			db.delete(CircleTableMetaData.TABLE_NAME, CircleTableMetaData._ID + "=" + circleId, null);
			//删除CIRCLEMEMBER
			db.delete(CircleMemberTableMetaData.TABLE_NAME, CircleMemberTableMetaData.CIRCLE_ID + "=" + circleId, null);
			db.setTransactionSuccessful();
		}finally{
			db.endTransaction();
		}
	}



	private ContentValues getContentValues(Circle circle){
		ContentValues values = new ContentValues();
		if(circle.getId() > 0){
			values.put(CircleTableMetaData._ID, circle.getId());
		}
		values.put(CircleTableMetaData.NAME, circle.getCircle_name());
		values.put(CircleTableMetaData.DESCRIPTION, circle.getCircle_description());
		values.put(CircleTableMetaData.ISACTIVE, circle.getActive());
		values.put(CircleTableMetaData.CREATE_AT, circle.getCircle_create_time());
		return values;
	}


}

